#include "bflb_mtimer.h"
#include "bflb_sec_sha.h"
#include "board.h"

/*
 * FIPS-180-1 test vectors
 */
static const unsigned char sha_test_buf[3][57] = {
    { "abc" },
    { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
    { "" }
};

static const size_t sha_test_buflen[3] = {
    3, 56, 1000
};

static const unsigned char sha1_test_sum[3][20] = {
    { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
      0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
    { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
      0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
    { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
      0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
};

static const unsigned char sha256_test_sum[6][32] = {
    /*
     * SHA-224 test vectors
     */
    {
        0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22,
        0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3,
        0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7,
        0xE3, 0x6C, 0x9D, 0xA7 },
    { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC,
      0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50,
      0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19,
      0x52, 0x52, 0x25, 0x25 },
    { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8,
      0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B,
      0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE,
      0x4E, 0xE7, 0xAD, 0x67 },

    /*
     * SHA-256 test vectors
     */
    {
        0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
        0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
        0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
        0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
    { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
      0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
      0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
      0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
    { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
      0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
      0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
      0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
};

/*
 * FIPS-180-2 test vectors
 */
static const unsigned char sha512_test_buf[3][113] = {
    { "abc" },
    { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
    { "" }
};

static const size_t sha512_test_buflen[3] = {
    3, 112, 1000
};

static const unsigned char sha512_test_sum[6][64] = {
    /*
     * SHA-384 test vectors
     */
    {
        0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B,
        0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07,
        0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
        0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED,
        0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23,
        0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 },
    { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8,
      0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47,
      0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2,
      0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12,
      0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9,
      0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 },
    { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB,
      0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C,
      0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52,
      0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B,
      0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB,
      0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 },
    /*
     * SHA-512 test vectors
     */
    {
        0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA,
        0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31,
        0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2,
        0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A,
        0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8,
        0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD,
        0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E,
        0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F },
    { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA,
      0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F,
      0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1,
      0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18,
      0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4,
      0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A,
      0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54,
      0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 },
    { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64,
      0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63,
      0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28,
      0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB,
      0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A,
      0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B,
      0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E,
      0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
};

ATTR_NOCACHE_NOINIT_RAM_SECTION __attribute__((aligned(32))) uint8_t sha_input_buf[1000]; /* input addr must be align 32 */

uint8_t sha_output_buf[128];

ATTR_NOCACHE_NOINIT_RAM_SECTION struct bflb_sha1_ctx_s ctx_sha1;
ATTR_NOCACHE_NOINIT_RAM_SECTION struct bflb_sha256_ctx_s ctx_sha256;
ATTR_NOCACHE_NOINIT_RAM_SECTION struct bflb_sha512_ctx_s ctx_sha512;

static uint32_t bflb_data_compare(const uint8_t *expected, uint8_t *input, uint32_t len)
{
    int i = 0;
    for (i = 0; i < len; i++) {
        if (input[i] != expected[i]) {
            printf("Compare fail at %d,input %02x, but expect %02x\r\n", i, input[i], expected[i]);
            while (1) {
            }
        }
    }

    return 0;
}

int main(void)
{
    board_init();

    struct bflb_device_s *sha;

    sha = bflb_device_get_by_name("sha");

    bflb_group0_request_sha_access(sha);

    bflb_sha_init(sha, SHA_MODE_SHA1);

    for (uint8_t i = 0; i < 3; i++) {
        bflb_sha1_start(sha, &ctx_sha1);
        if (i == 2) {
            memset(sha_input_buf, 'a', 1000);
            for (uint32_t j = 0; j < 1000; j++) {
                bflb_sha1_update(sha, &ctx_sha1, sha_input_buf, sha_test_buflen[i]);
            }
        } else {
            memcpy(sha_input_buf, sha_test_buf[i], sha_test_buflen[i]);
            bflb_sha1_update(sha, &ctx_sha1, sha_input_buf, sha_test_buflen[i]);
        }

        bflb_sha1_finish(sha, &ctx_sha1, sha_output_buf);
        bflb_data_compare(sha1_test_sum[i], sha_output_buf, 20);
    }

    printf("sha1 success\r\n");

    bflb_sha_init(sha, SHA_MODE_SHA224);
    for (uint8_t i = 0; i < 3; i++) {
        bflb_sha256_start(sha, &ctx_sha256);
        if (i == 2) {
            memset(sha_input_buf, 'a', 1000);
            for (uint32_t j = 0; j < 1000; j++) {
                bflb_sha256_update(sha, &ctx_sha256, sha_input_buf, sha_test_buflen[i]);
            }
        } else {
            memcpy(sha_input_buf, sha_test_buf[i], sha_test_buflen[i]);
            bflb_sha256_update(sha, &ctx_sha256, sha_input_buf, sha_test_buflen[i]);
        }

        bflb_sha256_finish(sha, &ctx_sha256, sha_output_buf);
        bflb_data_compare(sha256_test_sum[i], sha_output_buf, 28);
    }
    printf("sha224 success\r\n");

    bflb_sha_init(sha, SHA_MODE_SHA256);
    for (uint8_t i = 0; i < 3; i++) {
        bflb_sha256_start(sha, &ctx_sha256);
        if (i == 2) {
            memset(sha_input_buf, 'a', 1000);
            for (uint32_t j = 0; j < 1000; j++) {
                bflb_sha256_update(sha, &ctx_sha256, sha_input_buf, sha_test_buflen[i]);
            }
        } else {
            memcpy(sha_input_buf, sha_test_buf[i], sha_test_buflen[i]);
            bflb_sha256_update(sha, &ctx_sha256, sha_input_buf, sha_test_buflen[i]);
        }

        bflb_sha256_finish(sha, &ctx_sha256, sha_output_buf);
        bflb_data_compare(sha256_test_sum[i + 3], sha_output_buf, 32);
    }
    printf("sha256 success\r\n");

    bflb_sha_init(sha, SHA_MODE_SHA384);
    for (uint8_t i = 0; i < 3; i++) {
        bflb_sha512_start(sha, &ctx_sha512);
        if (i == 2) {
            memset(sha_input_buf, 'a', 1000);
            for (uint32_t j = 0; j < 1000; j++) {
                bflb_sha512_update(sha, &ctx_sha512, sha_input_buf, sha512_test_buflen[i]);
            }
        } else {
            memcpy(sha_input_buf, sha512_test_buf[i], sha512_test_buflen[i]);
            bflb_sha512_update(sha, &ctx_sha512, sha_input_buf, sha512_test_buflen[i]);
        }

        bflb_sha512_finish(sha, &ctx_sha512, sha_output_buf);
        bflb_data_compare(sha512_test_sum[i], sha_output_buf, 48);
    }
    printf("sha384 success\r\n");

    bflb_sha_init(sha, SHA_MODE_SHA512);
    for (uint8_t i = 0; i < 3; i++) {
        bflb_sha512_start(sha, &ctx_sha512);
        if (i == 2) {
            memset(sha_input_buf, 'a', 1000);
            for (uint32_t j = 0; j < 1000; j++) {
                bflb_sha512_update(sha, &ctx_sha512, sha_input_buf, sha512_test_buflen[i]);
            }
        } else {
            memcpy(sha_input_buf, sha512_test_buf[i], sha512_test_buflen[i]);
            bflb_sha512_update(sha, &ctx_sha512, sha_input_buf, sha512_test_buflen[i]);
        }

        bflb_sha512_finish(sha, &ctx_sha512, sha_output_buf);
        bflb_data_compare(sha512_test_sum[i + 3], sha_output_buf, 64);
    }
    printf("sha512 success\r\n");
    bflb_group0_release_sha_access(sha);
    while (1) {
        bflb_mtimer_delay_ms(2000);
    }
}